package com.fpt.quanlysanbong.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.fpt.quanlyhethongsan.utils.Constants;
import com.fpt.quanlysanbong.domain.SoccerField;
import com.fpt.quanlysanbong.domain.SoccerService;

public class ServiceDao extends BaseDao {
	public List<SoccerService> getListService(int soccerFieldId) {
		String sql = "Select * From SoccerService where soccerFieldId = " + soccerFieldId;
		List<SoccerService> listSoccerService = new ArrayList<SoccerService>();
		try {
			ResultSet rs = conn.createStatement().executeQuery(sql);
			HttpSession session = ServletActionContext.getRequest().getSession();
			SoccerField soccerField = (SoccerField) session.getAttribute(Constants.CURRENT_FIELD);

			while (rs.next()) {
				SoccerService soccerService = new SoccerService();
				soccerService.setSoccerField(soccerField);
				soccerService.setId(rs.getInt("id"));
				soccerService.setName(rs.getString("name"));
				soccerService.setUnitPrice(rs.getFloat("unitPrice"));

				listSoccerService.add(soccerService);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			closeConnection();
		}

		return listSoccerService;
	}

	public SoccerService getSoccerService(int id) {
		String sql = "Select * From SoccerService where id = " + id;
		SoccerService soccerService = null;
		try {
			ResultSet rs = conn.createStatement().executeQuery(sql);

			while (rs.next()) {
				soccerService = new SoccerService();
				soccerService.setId(rs.getInt("id"));
				soccerService.setName(rs.getString("name"));
				soccerService.setUnitPrice(rs.getFloat("unitPrice"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} 

		return soccerService;
	}

	public boolean addService(SoccerService soccerService) {
		boolean success = false;

		String sql1 = "Insert into SoccerService(name, soccerFieldId, unitPrice) values (?,?,?)";

		try {
			conn.setAutoCommit(false);

			prst = conn.prepareStatement(sql1);
			prst.setString(1, soccerService.getName());
			prst.setInt(2, soccerService.getSoccerField().getId());
			prst.setFloat(3, soccerService.getUnitPrice());

			int affectedRows = prst.executeUpdate();
			if (affectedRows == 0) {
				throw new SQLException("Creating Service failed, no rows affected.");
			}
			conn.commit();
			success = true;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			closeConnection();
		}

		return success;
	}

	public boolean editService(SoccerService soccerService) {
		boolean success = false;

		String sql1 = "UPDATE SoccerService SET name = ?, unitPrice = ? WHERE id = ?";

		try {
			conn.setAutoCommit(false);

			prst = conn.prepareStatement(sql1);
			prst.setString(1, soccerService.getName());
			prst.setFloat(2, soccerService.getUnitPrice());
			prst.setInt(3, soccerService.getId());

			prst.executeUpdate();
			conn.commit();
			success = true;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			closeConnection();
		}

		return success;
	}

	public boolean deleteService(int id) {
		boolean success = false;

		String sql1 = "DELETE FROM SoccerService WHERE id = ?  ";

		try {
			conn.setAutoCommit(false);

			prst = conn.prepareStatement(sql1);
			prst.setInt(1, id);

			prst.executeUpdate();
			conn.commit();
			success = true;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			closeConnection();
		}

		return success;
	}
}
